Imports System.Data.OleDb

Public Class frmSpaziatureStampaContratti

    Dim rsDataset As New DataSet
    Dim daAdattatore As OleDbDataAdapter
    Dim cmBuild As OleDbCommandBuilder

    Private Sub frmSpaziatureStampaContratti_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        rsDataset = Nothing
        daAdattatore = Nothing
    End Sub

    Private Sub frmSpaziatureStampaContratti_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cnNW As New OleDb.OleDbConnection(strConnessioneClienti)
        Dim strSql As String = "SELECT KEY, NOME AS Campo, ORIZZONTALE AS [Spazio orizzontale], VERTICALE AS [Spazio verticale] FROM SPAZIATURE"

        daAdattatore = New OleDb.OleDbDataAdapter(strSql, cnNW)
        cmBuild = New OleDbCommandBuilder(daAdattatore)
        daAdattatore.MissingSchemaAction = MissingSchemaAction.AddWithKey

        daAdattatore.Fill(rsDataset, "elenco")
        If rsDataset.Tables("elenco").Rows.Count <> 0 Then
            flxGriglia.DataSource = Nothing
            flxGriglia.DataSource = rsDataset.Tables("elenco")
            flxGriglia.Columns(0).Visible = False
            flxGriglia.Columns(1).Width = 170
            flxGriglia.Columns(1).ReadOnly = True
            cnNW.Close()
        Else
            MsgBox("Errore nel caricamento dei dati", MsgBoxStyle.Information, "Spaziature stampa contratti")
            Me.Close()
        End If
    End Sub

    Private Sub flxGriglia_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles flxGriglia.CellEndEdit
        ' salva la modifica
        Dim keyId As Integer
        Dim nome As String
        Dim valore As Object
        Dim dtRow As DataRow

        If IsDBNull(flxGriglia.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then Exit Sub

        keyId = flxGriglia.Rows(e.RowIndex).Cells(0).Value
        nome = flxGriglia.Columns(e.ColumnIndex).HeaderText
        valore = flxGriglia.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

        If Not IsNumeric(valore) Then
            MsgBox("Inserire solo valori numerici", MsgBoxStyle.Critical, "Attenzione")
            Exit Sub
        End If

        dtRow = rsDataset.Tables(0).Rows.Find(keyId)
        dtRow.BeginEdit()
        dtRow.Item(nome) = valore.ToString
        dtRow.EndEdit()

        daAdattatore.Update(rsDataset, "elenco")

    End Sub

End Class